#include "ScrollingLabel.h"
#include <QPainter>
#include <QFontMetrics>


ScrollingLabel::ScrollingLabel(QWidget *parent)
    : QLabel(parent),
    m_scrollType(LeftToRight),
    m_scrollOffset(0),
    m_delayTimer(new QTimer(this)),
    m_animation(new QPropertyAnimation(this, "scrollOffset")),
    m_textWidth(0)
{
    connect(m_animation, &QPropertyAnimation::finished, this, &ScrollingLabel::restartAnimation);
}

void ScrollingLabel::setScrollText(const QString &text)
{
    m_text = text;
    QFontMetrics metrics(font());
    m_textWidth = metrics.horizontalAdvance(m_text); // 更新文本宽度
    updateFontSize();
    update();
}

void ScrollingLabel::setScrollType(ScrollType type)
{
    m_scrollType = type;
    setupAnimation();
}

void ScrollingLabel::startScrolling()
{
    if (m_animation->state() == QAbstractAnimation::Running) {
        m_animation->stop();
    }

    m_scrollOffset = - m_textWidth; // 重置偏移量
    setupAnimation();
    m_animation->start();
}

void ScrollingLabel::stopScrolling()
{
    m_animation->stop();
    m_delayTimer->stop();
}

int ScrollingLabel::scrollOffset() const
{
    return m_scrollOffset;
}

void ScrollingLabel::setScrollOffset(int offset)
{
    m_scrollOffset = offset;
    update();
}

int ScrollingLabel::visibleTextLength() const
{
    return m_visibleTextLength;
}

void ScrollingLabel::setVisibleTextLength(int length)
{
    if (m_visibleTextLength != length) {
        m_visibleTextLength = length;
        emit visibleTextLengthChanged();
        update(); // 更新绘制
    }
}

void ScrollingLabel::setScrollSpeed(int speed)
{
    if (speed <= 0) {
        return;
    }
    m_scrollSpeed = speed;
    if (m_animation && m_animation->state() == QAbstractAnimation::Running) {
        setupAnimation(); // 重新配置动画
        m_animation->start();
    }
}

void ScrollingLabel::resizeEvent(QResizeEvent *event)
{
    QLabel::resizeEvent(event);
    updateFontSize();
    setupAnimation();
}

void ScrollingLabel::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::TextAntialiasing);

    int textHeight = fontMetrics().height();
    int y = (height() + textHeight) / 2 - 2;

    if (m_scrollType == LeftToRight) {
        painter.drawText(-m_scrollOffset, y, m_text);
    } else if (m_scrollType == RightToLeft) {
        painter.drawText(width() - m_scrollOffset, y, m_text);
    }
}
void ScrollingLabel::updateFontSize()
{
    QFont f = font();
    f.setPointSize(height() / 2); // 动态调整字体大小
    setFont(f);

    QFontMetrics metrics(f);
    m_textWidth = metrics.horizontalAdvance(m_text); // 更新文本宽度
}


void ScrollingLabel::setupAnimation()
{
    if (!m_animation)
        return;

    if (m_animation->state() == QAbstractAnimation::Running) {
        return;
    }

    m_animation->stop();
    int totalDistance = m_textWidth /*+ width()*/; // 总滚动距离

    if (totalDistance <= 0) {
        return;
    }
    int duration = (totalDistance * 1000) / m_scrollSpeed; // 动画持续时间 (ms)

    m_animation->setDuration(duration);

    if (m_scrollType == LeftToRight) {
        m_animation->setStartValue(-width());
        m_animation->setEndValue(totalDistance);
    } else if (m_scrollType == RightToLeft) {
        m_animation->setStartValue(totalDistance);
        m_animation->setEndValue(-width());
    }

    m_animation->setEasingCurve(QEasingCurve::Linear);
}

void ScrollingLabel::restartAnimation()
{
    // 动画结束后延迟 1 秒再重新开始
    m_delayTimer->singleShot(500, this, [this]() {
        m_animation->start();
    });
}
